package com.gx.controller;

import com.alibaba.fastjson.JSONArray;
import com.gx.aspect.annotation.OperationLogger;
import com.gx.domain.SysMenu;
import com.gx.page.TableDataInfo;
import com.gx.response.R;
import com.gx.response.tree.BootstrapUiTreeNode;
import com.gx.service.api.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/menus")
public class MenuController extends BaseController {

    @Autowired
    private SysMenuService sysMenuService;

    @GetMapping
    public R list() {
        startPage();
        List<SysMenu> sysMenus = sysMenuService.queryAll();
        TableDataInfo dataTable = getDataTable(sysMenus);
        return R.ok().put("rows", dataTable.getRows()).put("total", dataTable.getTotal());
    }

    @OperationLogger(option = "新增菜单")
    @PostMapping
    public R add(@RequestBody SysMenu sysMenu){
        sysMenuService.save(sysMenu);
        return R.ok("新增成功");
    }

    @OperationLogger(option = "更新菜单")
    @PutMapping
    public R update(@RequestBody SysMenu sysMenu){
        sysMenuService.update(sysMenu);
        return R.ok("更新成功");
    }

    @GetMapping("/jsonMenuTree")
    public R lists() {
        List<SysMenu> sysMenus = sysMenuService.queryAll();
        return R.ok().put("data", sysMenus);
    }

//    @PostMapping("/json")
//    public R json(){
//        JSONArray menuJsonList = sysMenuService.getMenuJsonList();
//        return R.ok().put("data",menuJsonList);
//    }

    @GetMapping("/selectMenuTree")
    public R menuTree() {
        List<BootstrapUiTreeNode> menuTree = sysMenuService.getSelectMenuTree();
        return R.ok().put("tree", menuTree);
    }

    @GetMapping("/checkedMenuTree")
    public R getCheckedMenuTree(@RequestParam("roleId") String roleId) {
        List<BootstrapUiTreeNode> menuTree = sysMenuService.getCheckedMenuTree(Long.parseLong(roleId));
        return R.ok().put("tree", menuTree);
    }
}
